/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.hop.ui.hopgui.perspective.explorer.file.types.audio;

import org.apache.hop.core.logging.LogChannel;
import org.apache.hop.ui.hopgui.HopGui;
import org.apache.hop.ui.hopgui.perspective.explorer.ExplorerFile;
import org.apache.hop.ui.hopgui.perspective.explorer.ExplorerPerspective;
import org.apache.hop.ui.hopgui.perspective.explorer.file.IExplorerFileTypeHandler;
import org.apache.hop.ui.hopgui.perspective.explorer.file.types.base.BaseExplorerFileTypeHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;

/** How do we handle an Drawio file in file explorer perspective? */
public class AudioExplorerFileTypeHandler extends BaseExplorerFileTypeHandler
    implements IExplorerFileTypeHandler {

  public AudioExplorerFileTypeHandler(
      HopGui hopGui, ExplorerPerspective perspective, ExplorerFile explorerFile) {
    super(hopGui, perspective, explorerFile);
  }

  private Browser wBrowser;

  // Render the Drawio file...
  //
  @Override
  public void renderFile(Composite composite) {
    // wBrowser
    wBrowser = new Browser(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    FormData fdBrowser = new FormData();
    fdBrowser.left = new FormAttachment(0, 0);
    fdBrowser.top = new FormAttachment(0, 0);
    fdBrowser.right = new FormAttachment(100, 0);
    fdBrowser.bottom = new FormAttachment(100, 0);
    wBrowser.setLayoutData(fdBrowser);

    // TODO: add bottom section to show status, size, changed dates, cursor position...
    // TODO: options for validation, pretty print, ...
    // TODO: options for reading the file with a various transform plugins
    // TODO: option to discard changes (reload from disk)
    // TODO: find in file feature, hook it up to the project find function
    //

    reload();
  }

  @Override
  public void save() {
    try {
      // Save the current explorer file ....
      //
      String filename = explorerFile.getFilename();

      // Save the file...
      /*
      try (OutputStream outputStream = HopVfs.getOutputStream(filename, false)) {
        outputStream.write(wText.getText().getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
      }
      */
      explorerFile.clearChanged();
      perspective.refresh(); // refresh the explorer perspective tree
      perspective.updateGui(); // Update menu options
    } catch (Exception e) {

    }
  }

  @Override
  public void reload() {
    try {
      // String contents = readTextFileContent("UTF-8");
      String path = explorerFile.getFilename();
      int index = path.lastIndexOf("/BOOT-INF/");
      String file = path.substring(index);
      wBrowser.setUrl("./audio/examples/minimalMilkdrop/index.html?file=" + file);
    } catch (Exception e) {
      LogChannel.UI.logError(
          "Error reading contents of file '" + explorerFile.getFilename() + "'", e);
    }
  }
}
